package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;

import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;

import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    @Autowired
    private WmSensitiveMapper sensitiveMapper;

    /**
     * 查询敏感词列表
     */
    @Override
    public ResponseResult findAll(WmSensitiveDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        Page<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<WmSensitive> wmSensitivePage = sensitiveMapper.selectPage(page, null);

        if (StringUtils.isNotBlank(dto.getName())) {
            lambdaQueryWrapper.like(WmSensitive::getSensitives, "%" + dto.getName() + "%");
        }

        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        page = page(page, lambdaQueryWrapper);


        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());

        responseResult.setData(wmSensitivePage.getRecords());

        return responseResult;
    }


    /**
     * 新增敏感词
     *
     * @param wmSensitive
     */
    @Override
    public ResponseResult addSensitive(WmSensitive wmSensitive) {
        // 检查参数
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer selectCount = sensitiveMapper.selectCount(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (selectCount > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        } else {
            wmSensitive.setCreatedTime(new Date());
            sensitiveMapper.insert(wmSensitive);
        }
        return ResponseResult.okResult(wmSensitive);
    }

    /**
     * 删除敏感词
     * @param id
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        sensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        Integer selectCount = sensitiveMapper.selectCount(Wrappers.<WmSensitive>lambdaQuery().select(WmSensitive::getSensitives));
        if (selectCount > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"名称重复");
        }
        this.updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
